package com.vanhon.engine.decrypt;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.URL;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/* compiled from: DecryptStream.java */
/* loaded from: classes.dex */
class DecryptHttpSource implements DecryptSource {
    private boolean VERBOSE = false;
    private String TAG = "";
    private long mTotalSize = -1;
    private InputStream mInputStream = null;
    private long mCurrentOffset = -1;
    private URL mURL = null;

    private void teardownConnection() {
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public boolean open(String str) {
        try {
            this.mURL = new URL(str);
            seekTo(0L);
            return true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e2) {
            e2.printStackTrace();
            return false;
        }
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public void close() {
        teardownConnection();
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public long length() throws IOException {
        return this.mTotalSize;
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public void seek(long j) throws IOException {
        if (this.mCurrentOffset != j) {
            seekTo(j);
        }
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public int read(byte[] bArr, int i, int i2) throws IOException {
        int readAt = readAt(this.mCurrentOffset, bArr, i, i2);
        if (readAt > 0) {
            return readAt;
        }
        return -1;
    }

    @Override // com.vanhon.engine.decrypt.DecryptSource
    public int read(byte[] bArr) throws IOException {
        int readAt = readAt(this.mCurrentOffset, bArr, 0, bArr.length);
        if (readAt > 0) {
            return readAt;
        }
        return -1;
    }

    private void seekTo(long j) throws IOException {
        Response execute;
        int code;
        int lastIndexOf;
        teardownConnection();
        try {
            URL url = this.mURL;
            OkHttpClient okHttpClient = new OkHttpClient();
            Request.Builder url2 = new Request.Builder().url(url);
            while (true) {
                if (j > 0) {
                    url2.addHeader("Range", "bytes=" + j + "-");
                }
                execute = okHttpClient.newCall(url2.build()).execute();
                code = execute.code();
                if (code != 300 && code != 301 && code != 302 && code != 303) {
                    break;
                }
            }
            if (code == 206) {
                String header = execute.header("Content-Range");
                this.mTotalSize = -1L;
                if (header != null && (lastIndexOf = header.lastIndexOf(47)) >= 0) {
                    try {
                        this.mTotalSize = Long.parseLong(header.substring(lastIndexOf + 1));
                    } catch (NumberFormatException unused) {
                    }
                }
            } else if (code != 200) {
                throw new IOException();
            } else {
                this.mTotalSize = execute.body().contentLength();
            }
            if (j > 0 && code != 206) {
                throw new IOException();
            }
            this.mInputStream = new BufferedInputStream(execute.body().byteStream());
            this.mCurrentOffset = j;
        } catch (IOException e) {
            this.mTotalSize = -1L;
            this.mInputStream = null;
            this.mCurrentOffset = -1L;
            throw e;
        }
    }

    private int readAt(long j, byte[] bArr, int i, int i2) {
        try {
            if (j != this.mCurrentOffset) {
                seekTo(j);
            }
            int read = this.mInputStream.read(bArr, i, i2);
            if (read == -1) {
                read = 0;
            }
            this.mCurrentOffset += read;
            if (this.VERBOSE) {
                Log.d(this.TAG, "readAt " + j + " / " + i2 + " => " + read);
            }
            return read;
        } catch (NoRouteToHostException e) {
            Log.w(this.TAG, "readAt " + j + " / " + i2 + " => " + e);
            return -1;
        } catch (IOException unused) {
            if (this.VERBOSE) {
                Log.d(this.TAG, "readAt " + j + " / " + i2 + " => -1");
            }
            return -1;
        } catch (Exception e2) {
            if (this.VERBOSE) {
                Log.d(this.TAG, "unknown exception " + e2);
                Log.d(this.TAG, "readAt " + j + " / " + i2 + " => -1");
            }
            return -1;
        }
    }
}
